package com.example.stream.demostream.controller;

import com.alibaba.fastjson.JSON;
import com.example.stream.demostream.entity.Person;
import com.example.stream.demostream.service.SysManagerService;
import com.zhe.shiro.domain.TokenDto;
import com.zhe.web.common.ApiResponse;
import com.zhe.web.common.R;
import com.zhe.web.exception.BizException;
import com.zhe.web.valid.SaveGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author zhonghuaen
 * @create 2021-09-10
 */
@Controller
@RequestMapping("/auth")
@Api(value = "用户登陆认证Controller", tags = "用户登陆认证")
public class HelloConteoller {

    Logger logger=LogManager.getLogger(HelloConteoller.class);

    @Autowired
    private StringRedisTemplate redisTemplate;

    private Map<String, String> map=new HashMap<String, String>(){

        {

            put( "Name" , "Unmi" );

            put( "QQ" , "1125535" );

        }

    };

    @ApiOperation(value = "获取用户中心登录地址", notes = "返回用户中心登录地址")
    @ResponseBody
    @PostMapping("/getLoginUrl1")
    public ApiResponse<String> getLoginUrl1(@ApiParam("应用地址") @Validated(SaveGroup.class) @RequestBody Person person) {
        if (person.getId().equals("1")){
            throw new BizException("try error");
        }
        logger.error(person.getName());
        String url = "www.baidu.com";
        return ApiResponse.success(url);
    }

    @ResponseBody
    @GetMapping("/getLoginUrl")
    @Cacheable(value = {"test"},key = "#root.methodName")
    public R getRedisCache(String zhe) {
        System.out.println("查询数据库");
        Person person = new Person();
        person.setId("1");
        person.setName("session");
        person.setPhone("18628871312");
        String name = map.get("Name");
        return R.ok().put("data",person);
    }

    @Autowired
    SysManagerService managerService;

    @ResponseBody
    @GetMapping("/getbcprPasswrod")
    public R getbcprPasswrod(HttpServletRequest request){
//        String encode = BCPEUtils.encode("123456");
//        Object loginUser = request.getSession().getAttribute("loginUser");
//        TokenDto person = JSON.parseObject(loginUser.toString(), TokenDto.class);
        managerService.testShiwu();
//        return R.ok().put("encode",person);
        return R.ok();
    }
}
